<?php 
	require_once("connector.php");
	require_once("baseobj.php");
	
	class Role extends BaseObj { 
		public $RoleId = 0;
		public $Name = "";
		public $Description = "";
		
		public function __construct() {
			
		}
		
		public function load($row){	
			$this->RoleId = $row["ROLEID"];
			$this->Name = $row["NAME"];
			$this->Description = $row["DESCRIPTION"];
		}
		
		public static function getAll(){
			$arr = array();
			$i = 0;
			$sql = "SELECT * FROM Role ORDER BY Name";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			$cnt = oci_execute($stmt);
			$r = null;
			if($cnt > 0) {
				while($row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS)) {
					$r = new Role();
					$r->load($row);
					$arr[$i] = $r;
					$i++;
				}
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $arr;
		}
		
		public static function getBySession($sessionId) { 
			$sql = "SELECT r.* 
					FROM Role r
						JOIN Users u ON r.RoleId = u.RoleId
						JOIN Sessions s ON u.UserId = s.UserId
					WHERE s.SID = :sid";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':sid', $sessionId);

			$cnt = oci_execute($stmt);
			$u = null;
			if($cnt > 0) {				
				$row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);				
				$u = new Role();
				$u->load($row);
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $u;
		} 
	}
?>	